<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='新闻案例-中楚鸿',active='project'">
<header th:replace="admin/header::headerFragment(${title},${active})"></header>

<link th:href="@{/plugin/ckeditor5/ckeditor.css}" rel="stylesheet"/>
<link th:href="@{/plugin/imageUpload/css/upload.css}" rel="stylesheet"/>
<link th:href="@{/plugin/dropify/css/dropify.min.css}" rel="stylesheet"/>

<body class="fixed-left">
<div id="wrapper">
    <div th:replace="admin/header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <h4 class="page-title">
                            <a style="font-size: 16px; color: #666666;" href="javascript:void(0)" onclick="window.history.go(-1);"><i class="fa fa-chevron-left" aria-hidden="true"></i>&nbsp;&nbsp;返回</a>
                        </h4>
                    </div>
                    <div class="col-md-12">
                        <form id="projectForm" role="form" novalidate="novalidate">
                            <input type="hidden" name="id" th:value="${project!=null and project.id!=null}?${project.id}: ''" id="id"/>
                            <input type="hidden" name="img" th:value="${project!=null and project.img !=null}?${project.img}: ''" id="img"/>

                            <div class="upload-box">
                                <label for="title">封面（最多上传1张图片）</label>
                                <input type="file" id="input-file-now" class="dropify"  th:attr="data-default-file=${project!=null and project.img !=null}?${project.img}: ''" />
                            </div>

                            <div class="row">
                                <div class="col-md-2">
                                    <div class="form-group ">
                                        <label for="subtype">类型</label>
                                        <select id="subtype" name="subtype" class="form-control">
                                            <option value="LONGHUA" th:selected="${project!=null and project.subtype=='LONGHUA'}?true:false">龙华区补贴项目</option>
                                            <option value="BAOAN" th:selected="${project!=null and project.subtype=='BAOAN'}?true:false">宝安区补贴项目</option>
                                            <option value="NANSHAN" th:selected="${project!=null and project.subtype=='NANSHAN'}?true:false">南山区补贴项目</option>
                                            <option value="FUTIAN" th:selected="${project!=null and project.subtype=='FUTIAN'}?true:false">福田区补贴项目</option>
                                            <option value="LUOHU" th:selected="${project!=null and project.subtype=='LUOHU'}?true:false">罗湖区补贴项目</option>
                                            <option value="LONGGANG" th:selected="${project!=null and project.subtype=='LONGGANG'}?true:false">龙岗区补贴项目</option>
                                            <option value="GUANGMING" th:selected="${project!=null and project.subtype=='GUANGMING'}?true:false">光明新区补贴项目</option>
                                        </select>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="title">标题</label>
                                <input id="title" type="text" class="form-control" placeholder="标题" name="title" required="" aria-required="true"
                                       th:value="${project!=null and project.title!=null }?${project.title}: ''"/>
                            </div>

                            <div class="form-group">
                                <label for="summary">简介</label>
                                <textarea id="summary"  name="summary" class="form-control" th:text="${project!=null and project.summary !=null}?${project.summary}: ''"></textarea>
                            </div>

                            <div class="form-group">
                                <label for="editor">正文</label>
                                <textarea id="editor" name="content"
                                          th:utext="${project!=null and project.title !=null}?${project.content}: ''"></textarea>
                            </div>

                            <div class="text-right">
                                <button type="submit" class="btn btn-success waves-effect waves-light"><i class="fa fa-check" aria-hidden="true"></i>保存</button>
                                <a href="javascript:void(0)" onclick="window.history.go(-1);" class="btn btn-warning waves-effect waves-light">
                                    <i class="fa fa-times"></i> <span>取消</span>
                                </a>
                            </div>
                        </form>
                    </div>

                </div>
                <div th:replace="admin/footer :: footer-content"></div>
            </div>
        </div>
    </div>
</div>
<div th:replace="admin/footer :: footer"></div>

<script th:src="@{//cdn.bootcss.com/jquery-toggles/2.0.4/toggles.min.js}"></script>
<script th:src="@{//cdn.bootcss.com/jquery-validate/1.15.1/jquery.validate.min.js}"></script>
<script th:src="@{//cdn.bootcss.com/jquery-validate/1.15.1/localization/messages_zh.min.js}"></script>
<script th:src="@{/plugin/imageUpload/js/upload.js}"></script>

<script src="/plugin/ckeditor5/translations/zh-cn.js"></script>
<script src="/plugin/ckeditor5/ckeditor.js"></script>
<script src="/plugin/dropify/js/dropify.js"></script>

<script type="text/javascript">
    /*<![CDATA[*/

    $('.dropify').dropify({
        messages: {
            'default': '点击或拖拽文件到这里',
            'replace': '点击或拖拽文件到这里来替换文件',
            'remove': '移除文件',
            'error': '对不起，你上传的文件太大了',
        }
    });

    // 文章封面start
    $("#uploadBtn").ajaxImageUpload({
        url: '/admin/attach/uploadImg', //上传的服务器地址
        data: {name: '文章头图'},
        maxNum: 1, //允许上传图片数量
        hidenInputName: 'img', // 上传成功后追加的隐藏input名，注意不要带[]，会自动带[]，不写默认和上传按钮的name相同
        zoom: false, //允许上传图片点击放大
        allowType: ["gif", "jpeg", "jpg", "bmp", 'png'], //允许上传图片的类型
        maxSize: 2, //允许上传图片的最大尺寸，单位M
        before: function () {
            //alert('上传前回调函数');
        },
        success: function (data) {
            //alert('上传成功回调函数');
            //console.log(data);
        },
        error: function (e) {
            //alert('上传失败回调函数');
            //console.log(e);
        }
    });
    // 文章封面end


    //富文本start
    class UploadAdapter {
        constructor(loader) {
            this.loader = loader;
        }

        upload() {
            return new Promise((resolve, reject) => {
                const data = new FormData();
                data.append('file', this.loader.file);
                data.append('allowSize', 10);//允许图片上传的大小/兆
                $.ajax({
                    url: '/admin/qiniu/uploadImg',
                    type: 'POST',
                    data: data,
                    dataType: 'json',
                    processData: false,
                    contentType: false,
                    success: function (data) {
                        console.log(data);
                        if (data.code == -1) {
                            resolve({
                                default: data.payload.url
                            });
                        } else {
                            reject(data.msg);
                        }

                    }
                });

            });
        }

        abort() {
        }
    }


    ClassicEditor.create(document.querySelector('#editor'), {
        language: 'zh-cn'
    }).then(editor => {
        //const toolbarContainer = document.querySelector('#a');
        //toolbarContainer.appendChild(editor.ui.view.toolbar.element);

        // 这个地方加载了适配器
        editor.plugins.get('FileRepository').createUploadAdapter = (loader) => {
            return new UploadAdapter(loader);
        }
    }).catch(error => {
        console.error(error);
    })
    //富文本end


    //表单提交
    var tale = new $.tale();
    // validate the comment form when it is submitted
    $('#projectForm').validate({
        submitHandler: function (form) {
            //$(".dropify-render img").attr('src');
            var imgUrlBase64 = $(".dropify-render img").attr('src');

            if(imgUrlBase64.indexOf("data:image/") != -1){
                $('#img').val(imgUrlBase64);
            }

            //$('#content-editor').val($('#text').val());
            //$("#projectForm #categories").val($('#multiple-sel').val());
            var params =  $("#projectForm").serialize();
            var url = '/admin/project/save';
            tale.post({
                url:url,
                data:params,
                //contentType:"application/json;charset=utf-8",
                success: function (result) {
                    if (result && result.success) {
                        tale.alertOk({
                            text:'保存成功',
                            then: function () {
                                setTimeout(function () {
                                    window.location.href = '/admin/project/list';
                                }, 500);
                            }
                        });
                    } else {
                        tale.alertError(result.msg || '保存失败');
                    }
                }
            });
        }
    });

    /*]]>*/
</script>

</body>
</html>